package com.deepexi.sample.controller;

import com.deepexi.sample.entity.data.DemoModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
public class MongoController {

    private String queryField = "title";

    @Resource
    private MongoTemplate mongoTemplate;

    @ResponseBody
    @RequestMapping(value = "mongo/{value}", produces = "application/json;charset=UTF-8")
    public String queryMongo(@PathVariable(name = "value") String value) {
        Query query = new Query(Criteria.where(queryField).is(value));
        DemoModel model = mongoTemplate.findOne(query, DemoModel.class);
        if (model == null) {
            return "null";
        }
        return model.getTitle();
    }

}
